import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores/userStore'
import { hrDynamicRoute, userDynamicRoute } from '@/router/dynamicRoute.js'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      meta: {
        requireAuth: true,
      },
      component: () => import('@/views/Layout.vue'),
      children: [
        {
          path: '/',
          name: 'home',
          component: () => import('@/views/homeView/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        {
          path: 'userInfo',
          name: 'userInfo',
          component: () => import('@/views/userInfo/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        // 这里可以添加更多的静态子路由
        // 用户端路由
        {
          path: '/positionList',
          name: 'positionList',
          component: () => import('@/views/positionList/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        {
          path: '/myInvitation',
          name: 'myInvitation',
          component: () => import('@/views/myInvitation/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        {
          path: '/applyManage',
          name: 'applyManage',
          component: () => import('@/views/applyManage/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        {
          path: '/deliveryProcess',
          name: 'deliveryProcess',
          component: () => import('@/views/deliveryProcess/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        // 管理员端路由
        {
          path: '/positionManage',
          name: 'positionManage',
          component: () => import('@/views/positionManage/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        {
          path: '/invitationManage',
          name: 'invitationManage',
          component: () => import('@/views/invitationManage/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        {
          path:'/positionCvCount',
          name:'positionCvCount',
          component: () => import('@/views/positionCvCount/index.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/cvPollManage',
          name: 'cvPollManage',
          component: () => import('@/views/CVPollManage/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        {
          path: '/dataAnalusis',
          name: 'dataAnalusis',
          component: () => import('@/views/dataAnalusis/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
        {
          path: '/todo',
          name: 'todo',
          component: () => import('@/views/todoManage/index.vue'),
          meta: {
            requireAuth: true,
          },
        },
      ],
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/loginPage/index.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/registerView/index.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      name: '404',
      component: () => import('@/components/404/index.vue'),
    },
  ],
})

let isDynamicRoutesAdded = false // 添加一个标志变量

// 路由权限
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const userRole = userStore.userRole

  // 确保动态路由添加完成
  if (!isDynamicRoutesAdded) {
    if (userRole === 'hr') {
      hrDynamicRoute.forEach(route => {
        const existingRoute = router.hasRoute(route.name)
        if (!existingRoute) {
          router.addRoute('layout', route)
        }
      })
    } else if (userRole === 'user') {
      userDynamicRoute.forEach(route => {
        const existingRoute = router.hasRoute(route.name)
        if (!existingRoute) {
          router.addRoute('layout', route)
        }
      })
    }
    isDynamicRoutesAdded = true
  }

  if (to.meta.requireAuth) {
    if (userRole) {
      // 检查用户角色是否匹配路由的角色要求
      if (to.meta.role && to.meta.role !== userRole) {
        next('/404') // 如果角色不匹配，重定向到 404 页面
      } else {
        next()
      }
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
